#!/usr/bin/env python3
import gzip
from collections import defaultdict
from datetime import datetime
import zd 
import sys

def extract(egin, end, html):
    if not html:
        return ''
    if egin is None:
        start = 0
    else:
        start = html.find(egin)
        if start >= 0:
            start += len(egin)+1
        else:
            return
    if end is not None:
        end = html.find(end, start)
    if end is None or end >= 0:
        return html[start:end].strip()


exist = defaultdict(int)
for pos,i in enumerate(sys.stdin):
    time = int(extract('sessionTimestamp":','"',i) or 0)//1000
    AndroidId = extract('AndroidId":','"',i)
    if time and AndroidId:
        if exist[AndroidId] > time:
            continue
        exist[AndroidId] = time

with zd.open("/root/7月x日.csv.zst","w") as out:
    for k,v in sorted(exist.items(),key=lambda x:x[0]):
        out.write(f"{k},{datetime.fromtimestamp(v)}\n".encode('utf8'))
